﻿using AutoMapper;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using VM.Common.Result;
using VM.Entity.EntityDto.Menu;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.Menu;

namespace VM.DAL.Menu
{
    public class MenuDAL : BaseDAL, IMenuDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }


        public MenuDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }



        public menuInfoDetailDto getMenuInfoById(int id)
        {
            sys_menu Res = _Client.Queryable<sys_menu>()
                           .Includes(a => a.createUser)
                           .Includes(a => a.updateUser)
                           .Includes(a => a.meta)
                           .InSingle(id);//
            menuInfoDetailDto EndRes = _IMapper.Map<sys_menu, menuInfoDetailDto>(Res);

            return EndRes;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool addMenu(sys_menu obj)
        {
            bool Res = _Client.InsertNav(obj)
                      .Include(a => a.meta)
                      .ExecuteCommand();
            return Res;
        }


        public List<sys_menu> getMenuTreeList(string name)
        {
            List<sys_menu> listRes = _Client.Queryable<sys_menu>()//.ToList();
                 .Includes(a => a.createUser)
                 .Includes(a => a.updateUser)
                 .Includes(a => a.meta)
                 .ToTree(it => it.children, it => it.parentId, 0, it => it.id);
            return listRes;

        }
        public PagingData<getMenuListDto> getMenuList(int pageIndex, int pageSize, string name)
        {

            var listRes = _Client.Queryable<sys_menu>()
                    .Includes(a => a.createUser)
                    .Includes(a => a.updateUser)
                    .Includes(a => a.meta)
            .WhereIF(name != "" && name != null, a => a.name.Contains(name))
            .Where(a => a.billStatus != 999)
            ;

            PagingData<sys_menu> result = new PagingData<sys_menu>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
            };

            PagingData<getMenuListDto> resend = _IMapper.Map<PagingData<sys_menu>, PagingData<getMenuListDto>>(result);
            return resend;

        }





    }
}
